
package com.projectmodule.dao;
import com.projectmodule.bean.*;
import java.sql.*;
import java.util.*;
import util.*;
public class TbProjectDAO {
    private Connection conn = null;
    private PreparedStatement pt = null;
    private ResultSet rs = null;
    private boolean flag = false;
    private String sql = "";
    private TbProject tbProject=null;
    private List<TbProject> tbProjectList=null;

    public boolean add(TbProject inTbProject){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="insert into Tb_Project(ProjectCode,ProjectName,ProjectStartTime,ProjectEndTime,ProjectAcceptanceTime,ContractCode,ProjectManager,TempProjectGroupCode) values(?,?,?,?,?,?,?,?)";
        pt = conn.prepareStatement(sql);
        
        pt.setString(1,inTbProject.getProjectCode());
        pt.setString(2,inTbProject.getProjectName());
        pt.setString(3,inTbProject.getProjectStartTime());
        pt.setString(4,inTbProject.getProjectEndTime());
        pt.setString(5,inTbProject.getProjectAcceptanceTime());
        pt.setString(6,inTbProject.getContractCode());
        pt.setString(7,inTbProject.getProjectManager());
        pt.setString(8,inTbProject.getTempProjectGroupCode());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public boolean del(int inProjectId){
        return UtilDAO.del("Tb_Project", "ProjectId", inProjectId);
    }
    public boolean edit(TbProject inTbProject){
        try{
            
        conn=ConnectDB.getSqlServerConnection();
        sql="update Tb_Project set ProjectId=?,ProjectCode=?,ProjectName=?,ProjectStartTime=?,ProjectEndTime=?,ProjectAcceptanceTime=?,ContractCode=?,ProjectManager=?,TempProjectGroupCode=? where ProjectId=?";
        pt = conn.prepareStatement(sql);
        
        pt.setInt(1,inTbProject.getProjectId());
        pt.setString(2,inTbProject.getProjectCode());
        pt.setString(3,inTbProject.getProjectName());
        pt.setString(4,inTbProject.getProjectStartTime());
        pt.setString(5,inTbProject.getProjectEndTime());
        pt.setString(6,inTbProject.getProjectAcceptanceTime());
        pt.setString(7,inTbProject.getContractCode());
        pt.setString(8,inTbProject.getProjectManager());
        pt.setString(9,inTbProject.getTempProjectGroupCode());
        pt.setInt(10,inTbProject.getProjectId());
        if (pt.executeUpdate() > 0) flag=true;
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return flag;
    }
    public TbProject getTbProjectByProjectId(int inProjectId) {
        tbProject=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Project where ProjectId=? ";
            pt = conn.prepareStatement(sql);
            pt.setInt(1,inProjectId);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbProject == null)tbProject = getTbProject(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return tbProject;
    }
    public TbProject getTbProjectByProjectCode(String inProjectCode) {
        tbProject=null;
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Project where ProjectCode=? ";
            pt = conn.prepareStatement(sql);
            pt.setString(1,inProjectCode);
            rs = pt.executeQuery();
            while (rs.next()) {
                if (tbProject == null)tbProject = getTbProject(rs);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return tbProject;
    }
    public List<TbProject> getTbProjectAll() {
        tbProjectList=new ArrayList<TbProject>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Project";
            pt = conn.prepareStatement(sql);
            rs = pt.executeQuery();
            while (rs.next()) {
                tbProjectList.add(getTbProject(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return tbProjectList;
    }
    public List<TbProject> getTbProjectAll(VskPage vp) {
        tbProjectList=new ArrayList<TbProject>();
        try{
            conn = ConnectDB.getSqlServerConnection();
            sql = "select * from Tb_Project";
            vp.setAllCountSql(sql);
            vp.getAllRecordCount(conn, vp.getAllCountSql());
            pt = conn.prepareStatement(vp.processPageSql( "Tb_Project", "ProjectId"));
            rs = pt.executeQuery();
            while (rs.next()) {
                tbProjectList.add(getTbProject(rs));
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            ConnectDB.closeSqlConnection();
        }
        return tbProjectList;
    }
    public TbProject getTbProject(ResultSet rs) throws Exception{
        TbProject tbProject = new TbProject();
        tbProject.setProjectId(rs.getInt("ProjectId"));
        tbProject.setProjectCode(rs.getString("ProjectCode"));
        tbProject.setProjectName(rs.getString("ProjectName"));
        tbProject.setProjectStartTime(rs.getString("ProjectStartTime"));
        tbProject.setProjectEndTime(rs.getString("ProjectEndTime"));
        tbProject.setProjectAcceptanceTime(rs.getString("ProjectAcceptanceTime"));
        tbProject.setContractCode(rs.getString("ContractCode"));
        tbProject.setProjectManager(rs.getString("ProjectManager"));
        tbProject.setTempProjectGroupCode(rs.getString("TempProjectGroupCode"));
        return tbProject;
    }
    public static void main(String[] args) {
    	List<TbProject> tpList=new TbProjectDAO().getTbProjectAll();
    	for(int i=0;i<tpList.size();i++){
    		TbProject tp=tpList.get(i);
    		System.out.println(tp.getTbContract().getContractName());
    		System.out.println(tp.getTbTempProjectGroup().getTempProjectGroupName());
    	}
	}
}
